package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"perm-guard/cache"
	"perm-guard/models"
	"strconv"
	"time"
)

func PeopleAdd(c *gin.Context) {
	peono := c.PostForm("peono")
	peoname := c.PostForm("peoname")
	peonum, _ := strconv.Atoi(c.PostForm("peonum"))
	if peono == "" || peoname == "" || peonum == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "内容不能为空",
		})
		return
	}
	data := models.People{
		PeoNo:   peono,
		PeoName: peoname,
		PeoNum:  peonum,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderAdd(c *gin.Context) {
	pid, _ := strconv.Atoi(c.PostForm("pid"))
	orderno := c.PostForm("orderno")
	ordername := c.PostForm("ordername")
	orderprice, _ := strconv.Atoi(c.PostForm("orderprice"))
	if orderno == "" || ordername == "" || orderprice == 0 || pid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
		})
		return
	}
	data := models.PeopleOrder{
		Pid:        pid,
		OrderNo:    orderno,
		OrderName:  ordername,
		OrderPrice: orderprice,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func PeopleList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))

	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	Offset := (page - 1) * size
	/*
		SELECT
		  people_orders.id AS OrderId,
		  peoples.peo_no AS PeoNo,
		  peoples.peo_name AS PeoName,
		  peoples.peo_num AS PeoNum,
		  people_orders.order_no AS OrderNo,
		  people_orders.order_name AS OrderName,
		  people_orders.order_price AS OrderPrice
		FROM
		  people_orders
		  LEFT JOIN peoples ON people_orders.pid = peoples.id
	*/
	var list []models.PeopleOrderList

	redis_key := "redis_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("people_orders").
			Select(
				"people_orders.id AS OrderId",
				"peoples.peo_no AS PeoNo",
				"peoples.peo_name AS PeoName",
				"peoples.peo_num AS PeoNum",
				"people_orders.order_no AS OrderNo",
				"people_orders.order_name AS OrderName",
				"people_orders.order_price AS OrderPrice",
			).
			Joins("LEFT JOIN peoples ON people_orders.pid = peoples.id").
			Offset(Offset).
			Limit(size).
			Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	var total int64
	models.DB.Model(&models.PeopleOrder{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"list":  list,
		"total": total,
	})
	return
}
func PeopleOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var list models.PeopleOrderList
	redis_key := "redis_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("people_orders").
			Select(
				"people_orders.id AS OrderId",
				"peoples.peo_no AS PeoNo",
				"peoples.peo_name AS PeoName",
				"peoples.peo_num AS PeoNum",
				"people_orders.order_no AS OrderNo",
				"people_orders.order_name AS OrderName",
				"people_orders.order_price AS OrderPrice",
			).
			Joins("LEFT JOIN peoples ON people_orders.pid = peoples.id").
			Where("people_orders.id=?", id).
			Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"list": list,
	})
	return
}
